package cn.wizzer.app.web.modules.controllers.platform.user.account;

import cn.wizzer.app.user.modules.models.enums.PayChannel;
import cn.wizzer.app.user.modules.models.enums.RechargeStatusEnum;
import cn.wizzer.app.user.modules.services.account.UserRechargeService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.StringUtil;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.param.ParamVerify;
import cn.wizzer.notify.WkNotifyUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.math.BigDecimal;

@IocBean
@At("/platform/user/account/recharge")
public class RechargeController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private UserRechargeService rechargeService;

    @Inject
    private WkNotifyUtil wkNotifyUtil;

    @At("")
    @Ok("beetl:/platform/user/account/recharge/index.html")
    @RequiresPermissions("user.account.recharge")
    public void index() {

    }

    @At("/add")
    @Ok("json")
    @RequiresPermissions("user.account.recharge.add")
    @SLog(tag = "手动充值", msg = "为用户:${args[0]}充值:${args[1]}")
    public Object addDo(@ParamVerify(required = true) String toUser,
                        @ParamVerify(required = true) String amount,
                        @Param(value = "currencyId", df = "default") String currencyId,
                        @ParamVerify(required = true) PayChannel channel) {
        rechargeService.manual(new BigDecimal(amount), toUser, StringUtil.getPlatformUid(), currencyId, channel);
        return Result.success();
    }

    @At("/examine")
    @Ok("json")
    @RequiresPermissions("user.account.recharge.examine")
    @SLog(tag = "确认手动充值", msg = ":${args[0]}")
    public Object examine(@Param("id") String id, @Param("sure") Boolean sure, String remark) {
        rechargeService.examine(id, sure, remark, "MANUAL", StringUtil.getPlatformUid());
        return Result.success();
    }

    @At
    @Ok("json:full")
    @RequiresPermissions("user.account.recharge")
    public Object data(@Param("conditionData") String conditionData,
                       @Param("orderNo") String orderNo,
                       @Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("status") RechargeStatusEnum status,
                       @Param("channel") PayChannel channel,
                       @Param("currencyId") String currencyId,
                       @Param("times") int times,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        Pagination pagination = rechargeService.listPage(conditionData, orderNo, searchKeyword, searchDate, status, channel, currencyId, times, pageNumber, pageSize);
        // 统计充值总额，根据搜索条件
        BigDecimal sum = rechargeService.searchSum(conditionData, orderNo, searchKeyword, searchDate, status, channel, currencyId, times);
        return Result.success().addData(Result.createMap("pagination", pagination, "sum", sum));
    }

}
